﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TMDT_Nhom1A.BUS;
namespace TMDT_Nhom1A.Manager
{
    public partial class DanhSachSanPham_Manager : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ConnectData();
            }
        }

        private void ConnectData()
        {
            if (Request.QueryString["ParentCatalogueID"] != null)
            {
                int ParentCatalogue = int.Parse(Request.QueryString["ParentCatalogueID"]);
                dllistproducts.DataSource = ElectricDevice_BUS.ListProductByParentCatalogue(ParentCatalogue);
                dllistproducts.DataBind();
            }
            if (Request.QueryString["ChildCatalogueID"] != null)
            {
                int ChildCatalogue = int.Parse(Request.QueryString["ChildCatalogueID"]);
                dllistproducts.DataSource = ElectricDevice_BUS.ListProductByChildCatalogue(ChildCatalogue);
                dllistproducts.DataBind();
            }

            lbCatalogue.Text = Request.QueryString["Name"];
        }

        protected void dllistproducts_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "lbLinkToDetail" || e.CommandName == "ibLinkToDetail" || e.CommandName == "btEdit")
            {
                string url = "ChiTietSanPham_Manager.aspx";
                if (Request.QueryString["ChildCatalogueID"] != null)
                {
                    url = url + "?ChildCatalogueID=" + Request.QueryString["ChildCatalogueID"].ToString() + "&Name=" + Request.QueryString["Name"].ToString() + "&ElectricDeviceID=" + e.CommandArgument;
                    Response.Redirect(url);
                }
                if (Request.QueryString["ParentCatalogueID"] != null)
                {
                    url = url + "?ParentCatalogueID=" + Request.QueryString["ParentCatalogueID"].ToString() + "&Name=" + Request.QueryString["Name"].ToString() + "&ElectricDeviceID=" + e.CommandArgument;
                    Response.Redirect(url);
                }
            }

            if (e.CommandName == "btDelete")
            {
                ElectricDevice_BUS.DeleteElectricDevice(e.CommandArgument.ToString());
                ConnectData();
            }
        }

        protected void dllistproducts_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item)
            {
                string NonDiscountPrice = "";
                string DiscountPrice = "";
                LinkButton ID = (LinkButton)e.Item.FindControl("lbLinkToDetail");
                Coupon_BUS.GetPriceCouponOfElectricDevice(ID.CommandArgument, ref NonDiscountPrice, ref DiscountPrice);
                if (NonDiscountPrice != "" && DiscountPrice != "")
                {
                    e.Item.FindControl("del").Visible = true;
                    e.Item.FindControl("nondel").Visible = false;
                    Label lbDiscountPrice = (Label)e.Item.FindControl("lbDiscountPrice");
                    Label lbNonDiscountPrice = (Label)e.Item.FindControl("lbNonDiscountPrice");
                    lbDiscountPrice.Text = DiscountPrice;
                    lbNonDiscountPrice.Text = NonDiscountPrice;
                    e.Item.FindControl("pPriceCoupon").Visible = true;
                    e.Item.FindControl("pNonDiscountPrice").Visible = true;
                }
                if (NonDiscountPrice == "" && DiscountPrice == "")
                {
                    e.Item.FindControl("del").Visible = false;
                    e.Item.FindControl("nondel").Visible = true;
                }
            }
        }
    }
}
